package
{
	import com.gengine.debug.*;
	import com.gengine.global.*;
	import com.gengine.resource.*;
	import com.gengine.ui.*;
	import com.gengine.utils.*;
	import com.mui.controls.*;
	import com.mui.core.*;
	import com.mui.events.*;
	import com.mui.manager.*;
	
	import extend.js.*;
	import extend.php.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	
	import mortal.common.font.*;
	import mortal.common.global.*;
	import mortal.common.preLoadPage.*;
	import mortal.component.skin.*;
	import mortal.component.window.*;
	import mortal.game.control.*;
	import mortal.game.manager.*;
	import mortal.game.mvc.*;
	import mortal.game.resource.*;
	import mortal.game.scene.map.*;
	import mortal.game.scene.modle.data.*;
	import mortal.game.view.common.alertwins.*;
	import mortal.game.view.common.tooltip.*;
	import mortal.game.scene.player.entity.*;
	
	public class MainGame extends Application
	{
		private var _libraryUrls:Array;
		
		public function MainGame()
		{
			this._libraryUrls = ["assets/ui/mainUI.swf", "assets/ui/imageLib.swf", "assets/ui/skillUI.swf", "assets/ui/Face.swf"];
			FontLibrary.getInstance();
			FramesData.setData();
		}
		
		override protected function initApp() : void
		{
			JSASSender.instance.start();
			SceneRange.screenResolutionX = Capabilities.screenResolutionX;
			SceneRange.screenResolutionY = Capabilities.screenResolutionY;
			LoaderManager.instance.maxQueue = 4;
			ParamsConst.instance.init(stage);
			Log.isSystem = false;
			if (!Global.isDebugModle)
			{
				Log.closeAll();
			}
			
			Log.isError = true;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.tabChildren = false;
			this.stage.stageFocusRect = false;
			this.initConfig();
		}
		
		private function initConfig() : void
		{
			ConfigManager.instance.addEventListener(Event.COMPLETE, this.onConfigCompleteHandler);
			ConfigManager.instance.addEventListener(ProgressEvent.PROGRESS, this.configLoadProgressHandler);
			ConfigManager.instance.addEventListener(ErrorEvent.ERROR, this.onConfigErrorHandler);
			ConfigManager.instance.load(PathConst.configUrl);
		}
		
		private function configLoadProgressHandler(event:ProgressEvent) : void
		{
			this.setProgress(PreloaderConfig.LOAD_CONFIG, event.bytesLoaded, event.bytesTotal);
		}
		
		private function onConfigErrorHandler(event:ErrorEvent) : void
		{
			if (PHPSender.isCreateRole)
			{
				PHPSender.sendToURLByPHP(SenderType.LoadConfigFail, event.text);
			}
		}
		
		private function onConfigCompleteHandler(event:Event) : void
		{
			var preloaders:Array = null;
			var url:String = null;
			if (PHPSender.isCreateRole)
			{
				PHPSender.sendToURLByPHP(SenderType.LoadConfigSuccess);
				if (ParamsConst.instance.preloaders != null && ParamsConst.instance.preloaders != "")
				{
					preloaders = ParamsConst.instance.preloaders.split(",");
					if (preloaders != null && preloaders.length > 0)
					{
						this._libraryUrls = this._libraryUrls.concat(preloaders);
					}
				}
			}
			GlobalClass.libaray.addEventListener(LibraryEvent.LOAD_COMPLETE, this.onlibraryCompleteHandler);
			GlobalClass.libaray.addEventListener(ProgressEvent.PROGRESS, this.resourceLoadProgressHandler);
			GlobalClass.libaray.addEventListener(ErrorEvent.ERROR, this.onLibraryErrorHandler);
			var i:int = 0;
			while (i < this._libraryUrls.length)
			{
				
				url = PathConst.mainPath + this._libraryUrls[i] + "?v=" + ParamsConst.instance.flashVersion;
				GlobalClass.libaray.loadSWF(url);
				i++;
			}
			Log.debug("初始化素材文件");
			this.initResouce();
		}
		
		private function onlibraryCompleteHandler(event:LibraryEvent) : void
		{
			if (PHPSender.isCreateRole)
			{
				PHPSender.sendToURLByPHP(SenderType.LoadResourceSuccess);
			}
			
			LayerManager.init();
			ToolTipsManager.init(LayerManager.toolTipLayer);
			ToolTipsManager.defaultRenderClass = Tooltip;
			DragManager.init(LayerManager.dragLayer);
			Alert.init(LayerManager.alertLayer);
			Alert.defaultAlertWinRenderer = CustomAlertWin;
			
			if (Global.isDebugModle)
			{
				Log.debugLog = DebugWindow.instance;
				Global.stage.addChild(FPS.instance);
			}
			
			GameController.init();
			GlobalSkin.initStyleSkin();
			SWFModelConfig.instance.init();
			GameLoadBar.instance.isGameLoadComplete = true;
			
			if (PreLoginProxy.instance.isLoginSuccess)
			{
				LoginController.instance.loadGame();
			}
			else
			{
				PreLoginProxy.instance.addEventListener(PreLoginProxy.LOGIN_SUCCESS, this.loginSuccessHandler);
			}
			
			var timerTest:TimerTest = new TimerTest(this.stage);
			timerTest.start();
			removeLoadListeners();
		}
		
		private function removeLoadListeners() : void
		{
			GlobalClass.libaray.removeEventListener(LibraryEvent.LOAD_COMPLETE, this.onlibraryCompleteHandler);
			GlobalClass.libaray.removeEventListener(ProgressEvent.PROGRESS, this.resourceLoadProgressHandler);
			GlobalClass.libaray.removeEventListener(ErrorEvent.ERROR, this.onLibraryErrorHandler);
			
		}
		
		private function loginSuccessHandler(event:Event) : void
		{
			PreLoginProxy.instance.removeEventListener(PreLoginProxy.LOGIN_SUCCESS, this.loginSuccessHandler);
			this.setProgress(PreloaderConfig.LOAD_RESCOUSE, 100, 100);
			LoginController.instance.loadGame();
		}
		
		
		private function resourceLoadProgressHandler(event:ProgressEvent) : void
		{
			this.setProgress(PreloaderConfig.LOAD_RESCOUSE, event.bytesLoaded, event.bytesTotal);
		}
		
		private function onLibraryErrorHandler(event:ErrorEvent) : void
		{
			if (PHPSender.isCreateRole)
			{
				PHPSender.sendToURLByPHP(SenderType.LoadResourceFail, event.text);
			}
			
		}
		
		private function initResouce() : void
		{
			var mask:SelectPlayerMask = new SelectPlayerMask();
			mask.scaleX = 0.5;
			mask.scaleY = 0.25;
			SpritePlayer.selectePlayerMask = mask;
		}
		
		private function setProgress(type:int, loaded:Number, total:Number) : void
		{
			GameLoadBar.instance.setProgress(type, loaded, total);
		}
	}
}